package com.helloworld.demo.threadbase;

public class ThreadWaitAndNotify {

    public static Object lock = new Object();
    public static boolean flag = true;

    public static void main(String[] args) {
        new Thread(new Runnable() {
            @Override
            public void run() {
                synchronized (lock){
                    while (flag){
                        try {
                            System.out.println(Thread.currentThread().getName() + "wait ......");
                            lock.wait();
                        } catch (InterruptedException e) {
                            e.printStackTrace();
                        }
                    }
                    System.out.println("wait end......");
                }
            }
        }).start();


        new Thread(new Runnable() {
            @Override
            public void run() {
                if(flag){
                    synchronized (lock){
                        if(flag){
                            try {
                                Thread.sleep(2000);
                            } catch (InterruptedException e) {
                                e.printStackTrace();
                            }
                            lock.notify();
                            System.out.println(Thread.currentThread().getName() + "notify......");
                            flag = false;
                        }
                    }
                }
            }
        }).start();
    }
}
